import { Notification } from 'element-ui'

/**
 * 地图模块 - 预报预警
 */
export default {
    data() {
        return {
            legend: [
                // { name: '紫', icon: require('@/assets/map/village/0.png') },
                { name: '红', icon: require('@/assets/map/village/1.png'), color: '#d03319' },
                { name: '橙', icon: require('@/assets/map/village/2.png'), color: '#e18130' },
                { name: '黄', icon: require('@/assets/map/village/3.png'), color: '#efdb3d' },
                { name: '蓝', icon: require('@/assets/map/village/4.png'), color: '#061dd0' }
            ],
            rivlWidth: 2,
            _rivlImg: require('@/assets/map/poly-rivers.png')
        }
    },
    // 计算属性
    computed: {
        // 流域编码
        basin() {
            return this.$store.getters.basin
        }
    },
    // 生命周期-创建
    created() {
        this.$eventBus.$on('flyToVillage_YJLJ', this.flyToVillShowPopYJLJ)
        // 监听tabchange事件
        this.$eventBus.$on('EB_YJ_TAB_Change', res => {
            // console.log(res)
            if (res.mdName === '临近预警') {
                // this.showMeshPredictionRain()
            } else {
                this.clearYJLJ()
            }
        })
        this.entitiesPonitYJLJ = new Map()
        this._dataSourceRivlYJLJ = []
        this._dataSourceLineYJLJ = new Cesium.CustomDataSource('lineYJLJ')
    },
    // 生命周期-销毁前
    beforeDestroy() {
    },
    // 生命周期-销毁
    destroyed() {
        // 清理
        this.clearYJLJ()
    },
    // 生命周期-挂载
    mounted() {
    },
    methods: {
        /**
         * 模块构建
         * @param {string} dtTime dtTime
         */
        creatYJLJ(dtTime) {
            console.log('模块构建-YJLJ')
            this.clearYJLJ()

            // 测试参数
            // dtTime = '2022-07-16 06:00'

            // var urlApi = '/api/EarlyWarning/GetAdcdRvcdList'
            var urlApi = '/api/EarlyWarning/GetAdcdRvcdByShort'
            var prarms = {
                wswscd: this.basin,
                dtTime: dtTime
            }
            this.$api.Get(urlApi, prarms).then(res => {
                if (res.data.length < 1) {
                    Notification({
                        message: `${dtTime}无临近预警`,
                        type: 'warning'
                    })
                    return
                }
                res.data.forEach(item => {
                    var Village = item.Village;
                    var lv = Number(item.WLevel) - 1;
                    var color = this.legend[lv].color;
                    var legend = this.legend[lv]
                    if (item.ewType === 1) {
                        // 村庄
                        var entity = {
                            eventName: 'YJLJ',
                            prop: item,
                            position: Cesium.Cartesian3.fromDegrees(
                                Number(item.Lgtd),
                                Number(item.Lttd)
                            ),
                            label: {
                                text: Village,
                                scale: 1,
                                font: '14px sans-serif',
                                fillColor: Cesium.Color.fromCssColorString(color),
                                outlineColor: Cesium.Color.fromCssColorString('#FFF'),
                                outlineWidth: 4,
                                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                                heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                                pixelOffset: new Cesium.Cartesian2(0, -20),
                                eyeOffset: new Cesium.Cartesian3(0, 0, 80),
                                horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 垂直
                                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                                disableDepthTestDistance: 50000000,
                                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000)
                            },
                            billboard: {
                                image: legend.icon,
                                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                                width: this.$config.mapConfig.billboardSize,
                                height: this.$config.mapConfig.billboardSize
                            }
                        }
                        this.entitiesPonitYJLJ.set(item.Ewcode, this.$viewer.entities.add(entity))
                    } else if (item.ewType === 3) {
                        var obj = {
                            ewcode: item.Ewcode,
                            color: legend.color,
                            item: item
                        }
                        // 河道
                        this.$eventBus.$emit('EB_YJ_LJ_CardSel_Res_RIVE', obj)
                        this._dataSourceRivlYJLJ.push(obj)
                    }
                })
                this.initEvevtYJLJ()
            })
        },

        /**
         * 模块事件初始化
         */
        initEvevtYJLJ() {
            var eventFn = (pickedObject, position) => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === 'YJLJ') {
                        this.flyToVillShowPopYJLJ(pickedObject.id.prop.Ewcode)
                    }
                    if (pickedObject.id.eventName === 'RIVL临近预警') {
                        // var cartesian = this.$viewer.camera.pickEllipsoid(event.position, this.$viewer.scene.globe.ellipsoid)
                        var cartesian = this.$viewer.camera.pickEllipsoid(position, this.$viewer.scene.globe.ellipsoid)
                        if (cartesian) {
                            // 将笛卡尔三维坐标转为地图坐标（弧度）
                            var cartographic = this.$viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian)
                            // 将地图坐标（弧度）转为十进制的度数
                            pickedObject.id.propByYbyj.lgtd = Number(Cesium.Math.toDegrees(cartographic.longitude).toFixed(5))
                            pickedObject.id.propByYbyj.lttd = Number(Cesium.Math.toDegrees(cartographic.latitude).toFixed(5))
                        }
                        const popStayParams = this.$store.getters.popStayParams
                        // if (!popParams.disable) { // 上一个对话框没有提交解禁
                        pickedObject.id.propByYbyj.eventName = 'RIVL临近预警'
                        popStayParams.name = '河道预警信息'
                        // popStayParams.posX = Math.round(event.position.x)
                        // popStayParams.posY = Math.round(event.position.y)
                        popStayParams.remarks = pickedObject.id.propByYbyj
                        popStayParams.remarks.tabel = '临近预警'
                        this.$store.dispatch('popStayParams', popStayParams)
                        // this.$eventBus.$emit('flyToRIVL', pickedObject.id.prop.Ewcode)
                    }
                }
            }
            this.$store.getters.eventCesiumOption.leftPickFns.set('YJLJ', eventFn)
        },
        flyToVillShowPopYJLJ(adcd) {
            var entity = this.entitiesPonitYJLJ.get(adcd)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 5000
                    }
                })
            }
            const popStayParams = this.$store.getters.popStayParams
            entity.prop.eventName = 'YJLJ'
            entity.prop.rainStart = this.ruleForm.dtBegin
            entity.prop.rainEnd = this.ruleForm.dtEnd
            popStayParams.name = '村庄预警信息'
            popStayParams.remarks = entity.prop
            this.$store.dispatch('popStayParams', popStayParams)
        },

        /**
         * 模块清理
         */
        clearYJLJ() {
            if (this.entitiesPonitYJLJ && this.entitiesPonitYJLJ.size > 0) {
                this.entitiesPonitYJLJ.forEach((val, key, map) => {
                    this.$viewer.entities.remove(val)
                    map.delete(key)
                })
                // this.entitiesPonitYJLJ = new Map()
            }
            this._dataSourceRivlYJLJ = []
            this.$eventBus.$emit('resetRIVLColor')
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')

            this.$store.getters.eventCesiumOption.leftPickFns.delete('YJLJ')
        }
    }
}
